home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / message.zip / SEND.C < prev    next >
C/C++ Source or Header  |  1992-04-12  |  3KB  |  102 lines

  1. //
  2. //              SEND.C    BY  Brian Hill
  3. //
  4. //      This program will take a message as a command line argument and
  5. // send this message out COM 1 in a format appropriate to the TSR program
  6. // MESSAGE.C.  If MESSAGE is installed on both machines, two way communication
  7. // is possible, otherwise SEND may be used.
  8. //
  9. //
  10.  
  11. #include <dos.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14.  
  15. /**        PROGRAM DEFINES          **/
  16. #define SOH 1                // Start Of Header
  17. #define EOT 4                // End OF Transmit
  18.  
  19. /**         FUNCTION PROTOTYPES     **/
  20. void init(void);             // setup serial port
  21. int chk_xmit();              // ok to send character out COM 1?
  22. void xmit_char(char ch);     // send char out COM 1.
  23.  
  24. int port;                    // serial port address
  25.  
  26. main(int argc, char **argv)
  27. {
  28.     char string[81];        // string to send
  29.     char *p;                // pointer to string
  30.     char ch;                // scratch variables
  31.     int i = 1;              //
  32.  
  33.     if(argc < 2)
  34.     {
  35.         printf("\nUSAGE:   send <message>");
  36.         exit(1);
  37.     }
  38.  
  39.     init();                 // set serial port up
  40.  
  41.     p = string;
  42.     for(;i<argc;i++)        // move message into string from command line
  43.     {
  44.         strcpy(p,argv[i]);   // copy next word
  45.         while(*p) p++;       // find end of string
  46.         *p++ = ' ';          // space between words
  47.     }
  48.     *p = 0;
  49.  
  50.     /* send string thru COM 1 */
  51.     p = string;
  52.     xmit_char(SOH);         // signal start of transmission
  53.     while(*p)
  54.     {
  55.        xmit_char(*p);       // sent this character
  56.        p++;                 // point to next character
  57.     }
  58.     xmit_char(EOT);         // specify end of transmission
  59.  
  60. } // end of main
  61.  
  62. void init(void)
  63. {
  64.     int _far *lp;           // pointer to DOS info
  65.     char ch;
  66.  
  67.     lp = (int far *) 0x00400000;    // pointer to DOS port info.
  68.     port = *lp;                     // get port address for COM 1
  69.  
  70.     ch = inp(port + 3);             // get UART line control register info
  71.  
  72.     /* port value for 8, N, 1 = xx 000 0 11 */
  73.     ch &= 0xC0;                     // save two highest bits (xx)
  74.     ch |= 0x03;                     // set 8 data bits
  75.     outp(port + 3, ch);             // set 8N1
  76.  
  77.     /* Set BPS rate to 9600  ::  115,200 / 12 = 9,600 BPS */
  78.     outp(port + 3, ch | 0x80);      // raise Data Latch Access Bit
  79.     outp(port    , 0x0C);           // write low byte of 12 decimal
  80.     outp(port + 1, 0x00);           // write high byte of 0
  81.  
  82.     ch = inp(port + 3);             // lower DLAB
  83.     outp(port + 3, ch & 0x7F);
  84.  
  85. } // end of init
  86.  
  87.  
  88. int chk_xmit()                  // wait 'til port available
  89. {
  90.     int status;
  91.  
  92.     status = inp(port + 5); // read line status register
  93.     return(status & 0x20);  // = xx1xxxxx binary when OK to send character
  94. }
  95.  
  96. void xmit_char(char ch)
  97. {
  98.     while(chk_xmit() == 0) {;}  // wait until port available
  99.     outp(port,ch);              //  and then send it out
  100. }
  101.  
  102.